import { _decorator, Component, Node, Prefab } from 'cc';
import { Joystick } from '../joystick/Joystick';
import { FightService } from './FightService';

const { ccclass, property } = _decorator;

@ccclass('PLayer')
export class PLayer extends Component {

    @property(Joystick)
    private joystick: Joystick;

    @property(Prefab)
    private firePrefab2: Prefab;

    private speed: number = 200;




    start() {

    }

    update(deltaTime: number) {
        const joyDir = this.joystick.joyDir;
        //向量归一化 恒定速度
        const dir = joyDir.normalize()

        //计算出的坐标
        const x = this.node.position.x;
        const y = this.node.position.y;

        const newX = x + dir.x * deltaTime * this.speed;
        const newY = y + dir.y * deltaTime * this.speed;

        this.node.setPosition(newX, newY);

        this.castSkill(deltaTime);
    }

    castSkill(deltaTime: number) {
        FightService.castSkill(this)
    }

    public get firePrefab(): Prefab {
        return this.firePrefab2;
    }
}


